package com.winsdom.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.winsdom.anno.Authentication;
import com.winsdom.annotation.OperationLog;
import com.winsdom.domain.BuildingMessage;
import com.winsdom.domain.PropertyInfoMessage;
import com.winsdom.domain.dto.AddFeeDTO;
import com.winsdom.domain.dto.PropertyInfoDTO;
import com.winsdom.domain.vo.FormulaPriceVO;
import com.winsdom.domain.po.MessagePO;
import com.winsdom.domain.vo.PropertyInfoFeeVO;
import com.winsdom.dto.CategoryTreeNodes;
import com.winsdom.entity.Building;
import com.winsdom.entity.Expenditure;
import com.winsdom.result.Result;
import com.winsdom.service.ProjectManagementService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import static com.winsdom.enums.AuthenticationType.PROJECT_MANAGEMENT;


@RestController
@RequestMapping("/project/management")
@Api(tags = "项目管理")
@Slf4j
@Authentication(value = PROJECT_MANAGEMENT)
public class ProjectManagementController {
    @Autowired
    private ProjectManagementService projectManagementService;

    /**
     * 获取组织架构树形结构
     * @return
     */
    @GetMapping("/tree")
    @Operation(summary = "获取组织树(该账号下的)", description = "获取组织、园区、楼盘、楼栋、楼层、资源的树形结构（先登录，自动获取账号id）")
    @OperationLog(description = "获取组织、园区、楼盘、楼栋、楼层、资源的树形结构(该账号下的)")
    public Result getOrganizationTree() {
        log.info("获取组织、园区、楼盘、楼栋、楼层、资源的树形结构....");
        List<CategoryTreeNodes> categoryTreeNodes=projectManagementService.getOrganizationTree();
        return Result.success(categoryTreeNodes);
    }

    /**
     *获取楼栋信息(分类表中的id)
     * @param id
     * @return
     */
    @GetMapping("/getBuildingMessage/{id}")
    @Operation(summary = "获取楼栋信息", description = "获取对应楼栋的信息")
    @OperationLog(description = "获取楼栋的信息")
    public Result getBuildingMessage(@Parameter(description = "分类id")  @PathVariable("id") Long id){
        log.info("要获取信息的对象，{}",id);
        BuildingMessage buildingMessage=projectManagementService.getBuildingMessage(id);
        return Result.success(buildingMessage);
    }

    /**
     *获取园区、楼层信息(分类表中的id)
     * @param id
     * @return
     */
    @GetMapping("/getMessage/{id}")
    @Operation(summary = "获取园区、楼层信息", description = "获取对应园区、楼层的信息")
    @OperationLog(description = "获取对应园区、楼层的信息")
    public Result getMessage(@Parameter(description = "分类id")  @PathVariable("id") Long id){
        log.info("要获取信息的对象，{}",id);
        MessagePO message=projectManagementService.getMessage(id);
        return Result.success(message);
    }

    /**
     *获取楼盘信息(分类表中的id)
     * @param id
     * @return
     */
    @GetMapping("/getPropertyInfoMessage/{id}")
    @Operation(summary = "获取楼盘信息", description = "获取对应楼盘的信息")
    @OperationLog(description = "获取对应楼盘的信息")
    public Result getPropertyInfoMessage(@Parameter(description = "分类id") @PathVariable("id")Long id){
        log.info("要获取信息的对象，{}",id);
        PropertyInfoMessage propertyInfoMessage=projectManagementService.getPropertyInfoMessage(id);
        return Result.success(propertyInfoMessage);
    }


    /**
     * 修改修改楼栋信息，及状态
     * @param building
     * @return
     */
    @PostMapping("/updateBuilding")
    @Operation(summary = "修改楼栋信息", description = "修改对应楼栋的信息")
    @OperationLog(description = "修改对应楼栋的信息")
    public Result updateBuilding(@Valid @Parameter(description = "只需要id和修改的名称") @RequestBody Building building){
        log.info("修改的数据为：{}",building);
        projectManagementService.updateBuilding(building);
        return Result.success("修改成功");
    }

    /**
     * 修改修改园区、楼层信息，及状态
     * @param messagePO
     * @return
     */
    @PostMapping("/updateMessage")
    @Operation(summary = "修改园区、楼层信息", description = "修改对应园区、楼层的信息")
    @OperationLog(description = "修改对应园区、楼层的信息")
    public Result updateMessage(@Valid @Parameter(description = "只需要id和修改的名称,父类id") @RequestBody MessagePO messagePO){
        log.info("修改的数据为：{}",messagePO);
        projectManagementService.updateMessage(messagePO);
        return Result.success("修改成功");
    }

    /**
     * 修改园区、楼盘、楼栋、楼层状态(楼栋表中的id)
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/updateStatus/{id}/{status}")
    @Operation(summary = "修改园区、楼盘、楼栋、楼层状态", description = "修改对应园区、楼盘、楼栋、楼层的状态")
    @OperationLog(description = "修改对应园区、楼盘、楼栋、楼层的状态")
    public Result updateStatus(@Parameter(description = "楼栋表中的id", required = true) @PathVariable("id") Long id,
                               @Parameter(description = "状态：1-启动 0-停用", required = true) @PathVariable("status") Integer status){
        log.info("修改的数据为：{},状态为：{}",id,status);
        projectManagementService.updateStatus(id,status);
        return Result.success("修改成功");
    }


    /**
     * 修改楼盘信息（楼盘表中的id）
     * @param propertyInfo
     * @return
     */
    @PostMapping("/updatePropertyInfo")
    @Operation(summary = "修改楼盘信息", description = "修改对应楼盘的信息")
    @OperationLog(description = "修改对应楼盘的信息")
    public Result updatePropertyInfo(@Valid @RequestBody PropertyInfoDTO propertyInfo){
        log.info("修改的楼盘数据为：{}",propertyInfo);
        projectManagementService.updatePropertyInfo(propertyInfo);
        return Result.success("修改成功");
    }


    /**
     * 查询楼盘关联费项(id为楼盘信息表中的id)
     * @param id
     * @return
     */
    @GetMapping("/getPropertyInfoFee/{id}")
    @Operation(summary = "查询楼盘关联费项", description = "查询楼盘关联费项数据")
    @OperationLog(description = "查询楼盘关联费项数据")
    public Result getPropertyInfoFee(@Parameter(description="需要查询的楼盘id",required = true) @PathVariable("id")Long id){
        log.info("需要查询计费关联楼盘的id：{}",id);
        PropertyInfoFeeVO propertyInfoFeeVO=projectManagementService.getPropertyInfoFee(id);
        return Result.success(propertyInfoFeeVO);
    }


    /**
     * 查询费项列表
     * @return
     */
    @GetMapping("/getexpenditureList")
    @Operation(summary = "查询费项列表", description = "查询费项列表数据")
    @OperationLog(description = "查询费项列表数据")
    public Result getExpenditureList(){
        List<Expenditure> expenditures=projectManagementService.getexpenditureList();
        return Result.success(expenditures);
    }


    /**
     * 根据费项id选择计费公式和单价
     * @param expenditureId
     * @return
     */
    @GetMapping("/getFormulaPriceList/{expenditureId}")
    @Operation(summary = "查询公式、价格列表", description = "根据费项id选择计费公式和单价")
    @OperationLog(description = "根据费项id选择计费公式和单价")
    public Result getFormulaPriceList(@Parameter(description="费项id",required = true) @PathVariable("expenditureId") Long expenditureId){
        log.info("需要查询计费公式和单价的费项id：{}",expenditureId);
        FormulaPriceVO formulaPriceVO=projectManagementService.getFormulaPriceList(expenditureId);
        return Result.success(formulaPriceVO);
    }


    /**
     * 添加费项
     * @param addFeeDTO
     * @return
     */
    @PostMapping("/add")
    @Operation(summary = "添加费项", description = "添加费项")
    @OperationLog(description = "添加费项")
    public Result add(@RequestBody AddFeeDTO addFeeDTO) throws JsonProcessingException {
        projectManagementService.add(addFeeDTO);
        return Result.success("添加成功");
    }


    /**
     * 移除费项（查询费项时有传入id）
     * @param billingAssociationId
     * @return
     */
    @GetMapping("/delete/{billingAssociationId}")
    @Operation(summary = "移除费项", description = "移除费项")
    @OperationLog(description = "移除费项")
    public Result delete(@Parameter(description="费项id",required = true) @PathVariable("billingAssociationId")Integer billingAssociationId){
        log.info("需要移除费项的id：{}",billingAssociationId);
        projectManagementService.delete(billingAssociationId);
        return Result.success("费项移除成功");
    }


}
